package com.justonetech.translation.controller;

import com.justonetech.core.controller.BaseCRUDActionController;
import com.justonetech.core.orm.hibernate.Page;
import com.justonetech.core.orm.hibernate.QueryTranslate;
import com.justonetech.core.utils.ReflectionUtils;
import com.justonetech.translation.daoservice.BillDetailService;
import com.justonetech.translation.domain.BillDetail;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @author: System
 */
@Controller
public class BillDetailController extends BaseCRUDActionController<BillDetail> {
    private static Log log = LogFactory.getLog(BillDetailController.class);

    @Autowired
	private BillDetailService billDetailService;

	@RequestMapping
    @ResponseBody
	public Page<BillDetail> grid(Page page, String condition) {
		try {
            page.setAutoCount(true);

            String hql = "from BillDetail t where 1=1 " + page.getOrderByString("t.id desc");

            QueryTranslate queryTranslate = new QueryTranslate(hql, condition);

            // 查询
            page = billDetailService.findByPage(page, queryTranslate.toString());
		} catch (Exception e) {
            log.error("error", e);
		}

        return page;
	}

    @RequestMapping
    @ResponseBody
    public Page<BillDetail> gridByBill(Page page, String condition, Long billId) {
        try {
            page.setAutoCount(true);

            if(billId != null || billId > 0) {
                String hql = "from BillDetail t where bill.id =" + billId + page.getOrderByString("t.id desc");

                QueryTranslate queryTranslate = new QueryTranslate(hql, condition);

                // 查询
                page = billDetailService.findByPage(page, queryTranslate.toString());

                List<BillDetail> list = page.getRows();

                Double totalAmout = 0D;

                for(BillDetail bean : list) {
                    totalAmout += bean.getAmount().doubleValue();
                }

                Map map = new HashMap();

                map.put("amount", totalAmout);
                map.put("payDate", "合计");

                page.setUserdata(map);
            }
        } catch (Exception e) {
            log.error("error", e);
        }

        return page;
    }

    @RequestMapping
    public String init(Model model, BillDetail entity) throws Exception {
        try {
            if(entity != null && entity.getId() != null) {
                entity = billDetailService.get(entity.getId());

                model.addAttribute("bean", entity);
            }
        } catch (Exception e) {
            log.error("error", e);
        }

        return "view/bil/billDetailEdit";
    }

    @RequestMapping
    public String view(Model model, Long id) {
        BillDetail billDetail = billDetailService.get(id);

        model.addAttribute("bean", billDetail);
        return "view/bil/billDetailView";
    }

    @RequestMapping
    public void save(HttpServletResponse response, Model model, @ModelAttribute("bean") BillDetail entity)
            throws Exception {
        try {
            String[] columns = new String[]{
                    "id",
                    "amount",
                    "payDate",
                    "handDate",
                    "handler"
            };

            BillDetail target;
            if (entity.getId() != null) {
                target = billDetailService.get(entity.getId());

                ReflectionUtils.copyBean(entity, target, columns);
            } else {
                target = entity;
            }

            billDetailService.save(target);
            sendSuccessJSON(response, "保存成功");
        } catch (Exception e) {
            log.error("error", e);
            super.processException(response, e);
        }
    }

    @RequestMapping
    public void delete(HttpServletResponse response, Long id) throws Exception {
        billDetailService.delete(id);

        sendSuccessJSON(response, "删除成功");
    }

    @RequestMapping
    @ResponseBody
    public BillDetail getDetail(Long id) {
        if(id != null && id > 0) {
            BillDetail bean = billDetailService.get(id);
            return bean;
        }

        return null;
    }
}